$m(function (args) {
    var _that = args;

    var showTip = function (msg) {
        $("<div/>").alert({
            msg: msg,
            title: TempoUtils.locale("mine.prompt")
        });
    }

    var initDialog = function () {
        var $dom = $('#dialog-casebase-tempo').find('form');
        $('#category').selectmenu({
            width: 300
        });

        //案例名称 描述不允许输入特殊字符
        var themeInput = $dom.find("input[name='name']");
        themeInput.validatebox({
            required: true,
            maxLength: 200,
            autofill: false,
            valids: [/^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$/],
            validTip: TempoUtils.locale("mine.templateTip")
        });

        var descInput = $dom.find("textarea[name='description']");
        descInput.validatebox({
            required: true,
            maxLength: 200,
            autofill: false,
            // valids: [/^[a-zA-Z0-9_!()+=:"";'',.\-?|`……：“”‘’？《》·~！（）。，；、\u4e00-\u9fa5]+$/],
            // validTip: TempoUtils.locale("mine.descTip")
        });

        //上传图标变换
        $(".fimeName-text").mouseover(function () {
            $(this).css("color", "#40A9FF"),
                $(this).css("background-color", "#F5F7FA")
        });
        $(".fimeName-text").mouseout(function () {
            $(this).css("color", ""),
                $(this).css("background-color", "")
        });
        $(".icon-end-change").mouseover(function () {
            $(this).css("background-position", "-432px -64px")
        });

        $(".icon-end-change").mouseout(function () {
            $(this).css("background-position", "")
        });
        //上传文件效果
        $(".uploadFile-btn").mouseover(function () {
            $(this).css("color", "#40A9FF")
        });
        $(".uploadFile-btn").mouseout(function () {
            $(this).css("color", "")
        });

        $dom.find("#code").val(TempoUtils.uuid());
        if (_that) {
            $dom.attr("action", contextPath + "/api/ai/sysconfig/casebase/update-self-template");
            $dom.find("input[name=path]").val(args.path);
            $dom.find("input[name=name]").val(args.name);
            $dom.find("input[name=author]").val(args.author);
            $dom.find("select[name=category]").selectmenu('setValue', args.category);
            $dom.find("textarea[name=description]").val(args.description);
            $(".uploadFile-btn").css("display", "none")
            $(".uploadfile-text").css("display", "inline-table");
            $dom.find("#pdfSpan").text(args.pdfFile);
            $dom.find("#pngSpan").text(args.pngFile);
            // $dom.find("#tappSpan").text(args.tappFile);
            $('#category').selectmenu("disable");
            $('.uploadfile-text').find("#endIcon").click(function () {
                $(this.parentElement.parentElement.parentElement).find(".uploadFile-btn").css("display", "block");
                $(this.parentElement.parentElement.parentElement).find(".uploadfile-text").css("display", "none");
                $(this.parentElement.parentElement.parentElement).find(".uploadfile-text").find(".file-text").text("");
            })
        }

        //更新上传文件
        $("input[type=file]").change(function (e) {
            var _that = this;
            var value = $(_that).val();
            if (value) {
                if (_that.files[0] && _that.files[0].size == 0) {
                    var id = $(_that).attr("id");
                    // showTip(TempoUtils.locale("mine." + id + "SizeError"));
                    showTip("文件为0KB，请检查！");
                    //上传失败，将值设置为空
                    $(_that).val("");
                    return;
                }
                if(_that.id == "pdfFile" && !_that.files[0].name.endsWith(".pdf")){
                    showTip("案例说明仅支持pdf格式的文件");
                    //上传失败，将值设置为空
                    $(_that).val("");
                    return;
                }
                if (_that.id == "pngFile") {
                    if(!_that.files[0].name.endsWith(".png") && !_that.files[0].name.endsWith(".jpg") && !_that.files[0].name.endsWith(".jpeg")){
                        showTip("案例图片仅支持png/jpg/jpeg格式的文件");
                        //上传失败，将值设置为空
                        $(_that).val("");
                        return;
                    }
                    if(_that.files[0].size > 632832){
                        // showTip(TempoUtils.locale("mine." + id + "SizeError"));
                        showTip("图片大小不能超过618KB");
                        //上传失败，将值设置为空
                        $(_that).val("");
                        return;
                    }
                }
                $(_that.parentElement).css("display", "none")
                var brotherDom = $(_that.parentElement).next();
                brotherDom.css("display", "inline-table");
                brotherDom.find(".file-text").text(_that.files[0].name);
                brotherDom.find(".icon-end").click(function () {
                    $(_that).val("");
                    brotherDom.find(".file-text").text("");
                    $(_that.parentElement).css("display", "block");
                    brotherDom.css("display", "none");
                });
                var files = e.currentTarget.files;
                if (!files || files.length == 0) {
                    return;
                }
                $("#fileMsg").text("");
            }
        });
        //文件上传
        $("input[type=file]").click(function () {
            var _that = this;
            var brotherDom = $(_that.parentElement).next();
            if (_that.files[0] && _that.files[0].name && _that.files[0].size > 0) {
                $(_that.parentElement).css("display", "none");
                brotherDom.css("display", "inline-table");
                brotherDom.find(".file-text").text(_that.files[0].name);
            } else {
                $(_that.parentElement).css("display", "block");
                brotherDom.css("display", "none");
                brotherDom.find(".file-text").text("");
            }
        });
        $dom.find("input[id=name]").unbind("keydown").bind("keydown", function (e) {
            if (e.keyCode == 13) {
                return false;
            }
        });
    }

    initDialog();

})

